package algorithm.sort;

import java.util.*;
import java.util.stream.Collectors;

public class Template {
    /*数组：使用Arrays.sort()方法*/
    public void arraysSort() {
        int[] arr = {5, 3, 8, 2, 7};
        Arrays.sort(arr);
        String string = Arrays.toString(arr);
        System.out.println(string);
    }

    /*数组对象：升序*/
    public void arrayObjectSort() {
        String[] strArr = {"apple", "banana", "cherry"};
        Arrays.sort(strArr);
        String string = Arrays.toString(strArr);
        System.out.println(string);
    }

    /*数组对象：降序*/
    public void arrayObjectSort1() {
        String[] strArr = {"apple", "banana", "cherry"};
        Arrays.sort(strArr, (s1, s2) -> s2.compareTo(s1)); // 和下面等价
        Arrays.sort(strArr, Comparator.reverseOrder());
        String string = Arrays.toString(strArr);
        System.out.println(string);
    }

    /*数组对象：降序*/
    public void collectionsSort() {
        String[] strArr = {"apple", "banana", "cherry"};
        Arrays.sort(strArr, (s1, s2) -> s2.compareTo(s1)); // 和下面等价
        Arrays.sort(strArr, Comparator.reverseOrder());
        String string = Arrays.toString(strArr);
        System.out.println(string);
    }

    static class Person implements Comparable<Person> {
        String name;
        int age;

        Person(String name, int age) {

        }

        @Override
        public int compareTo(Person other) {
            return Integer.compare(this.age, other.age);
        }
    }

    public void priorityQueueSort() {

    }

    /*一个Student对象，对象属性为no,name,age,sex,height根据no升序排列，再根据name降序排列，再根据age降序排*/
    static class Student {
        int no;
        String name;
        int age;
        String sex;
        double height;

        Student(int no, String name, int age, String sex, double height) {
            this.no = no;
            this.name = name;
            this.age = age;
            this.sex = sex;
            this.height = height;
        }

        @Override
        public String toString() {
            return "Student{" + "no=" + no + ", name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + ", height=" + height + '}';
        }
    }

    public void studentSort() {
        List<Student> students = List.of(new Student(3, "Alice", 25, "Female", 165.5), new Student(1, "Bob", 30, "Male", 175.0), new Student(2, "Charlie", 28, "Male", 180.2), new Student(2, "David", 28, "Male", 178.5), new Student(2, "Eve", 28, "Female", 162.0));
        List<Student> newList = new ArrayList<>(students);
        newList.sort(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int noComparison = Integer.compare(s1.no, s2.no);
                if (noComparison != 0) {
                    return noComparison;
                }
                int nameComparison = s2.name.compareTo(s1.name);
                if (nameComparison != 0) {
                    return nameComparison;
                }
                return Integer.compare(s2.age, s1.age);
            }
        });

        for(Student student : newList){
            System.out.println(student);
        }
    }

    static class Student1 {
        String name;
        int age;
        String sex;

        Student1(String name, int age, String sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
    }

    public void studentSort1() {
        List<Student1> students = new ArrayList<>();
        students.add(new Student1("Alice", 20, "Female"));
        students.add(new Student1("Bob", 22, "Male"));
        students.add(new Student1("Cindy", 19, "Female"));
        students.add(new Student1("David", 21, "Male"));

        Map<String, List<Student1>> studentMap = students.stream().collect(Collectors.groupingBy(s -> s.sex));
    }
    /*Lambda groupingBy 多级分组*/
    public void groupingByExample(){
        List<Person> people = new ArrayList<>();
        people.add(new Person("Alice", 25));
        people.add(new Person("Bob", 30));
        people.add(new Person("Charlie", 25));
        people.add(new Person("David", 35));

        /*先按照年龄分组，然后在每个年龄组内再按照名字的首字母分组*/
        Map<Integer, Map<Character, List<Person>>> groupedByAgeAndInitial = people.stream().collect(Collectors.groupingBy(p -> p.age, Collectors.groupingBy(person -> person.name.charAt(0))));
        for (Map.Entry<Integer, Map<Character, List<Person>>> ageEntry : groupedByAgeAndInitial.entrySet()){
            System.out.println("Age: " + ageEntry.getKey());
            for (Map.Entry<Character, List<Person>> initialEntry : ageEntry.getValue().entrySet()) {
                System.out.println("  Initial: " + initialEntry.getKey());
                for (Person person : initialEntry.getValue()) {
                    System.out.println("    " + person.name);
                }
            }
        }
    }
}
